const Router = require('koa-router');
const tool = require("../../tools/tool");
const router = new Router();
const {db, Op, QueryTypes} = require("../../models/db");
// 开票页面 扫描单据加载明细
router.post('/bill/getDetailByScan', async ctx => {
    try {
        let user = ctx.user
        let body = ctx.request.body
        let tenantId = user.tenantId;
        let code = body.code;
        let codearr = code.split('-');
        if (codearr.length !== 2) return ctx.err('二维码识别错误')
        let transType = tool.xe.toNumber(codearr[0]);
        let billId = tool.xe.toNumber(codearr[1]);
        if (transType === 160001) {
            let card = await db.models['card'].findOne({
                where: {
                    tenantId,
                    id: billId
                }
            })
            if (!card) {
                return ctx.err('未找到凭证');
            }
            let list = await db.models['cardInfo'].findAll({
                where: {
                    cardId: card.id
                }
            })
            let ret = card.toJSON()
            ret.infos = list;
            return ctx.suc('', ret);
        }
        else {
            let sql = `
                select * from v_invoice where tenantId=${tenantId} and id=${billId} 
            `
            let vInvoice = await db.query(sql, { type: QueryTypes.SELECT, plain: true })
            if (!vInvoice) {
                return ctx.err('未找到票据')
            }
            let id = vInvoice.id;
            let billType = vInvoice.billType;
            if (['SALE', 'PUR', 'OI', 'OO'].indexOf(billType) !== -1) {
                vInvoice.infos = await db.models['invoiceInfo'].findAll({
                    where: {
                        iid: id
                    }
                })
            }
            if (['PAYMENT', 'RECEIPT'].indexOf(billType) !== -1) {
                vInvoice.infos = await db.models['invoiceReceipt'].findAll({
                    where: {
                        iid: id
                    }
                })
            }
            if (billType === 'TRANS') {
                return ctx.err('');
            }
            if (billType === 'YSG') {
                vInvoice.infos = await db.models['invoiceInfoYs'].findAll({
                    where: {
                        iid: id
                    }
                })
            }
            if (['YFK', 'YSK', 'SR', 'ZC'].indexOf(billType) !== -1) {
                vInvoice.infos = await db.models['invoiceOther'].findAll({
                    where: {
                        iid: id
                    }
                })
            }
            if (['YS', 'YF'].indexOf(billType) !== -1) {
                vInvoice.infos = await db.models['invoiceOther'].findAll({
                    where: {
                        iid: id
                    }
                })
            }
            if (billType === 'ZZ') {
                vInvoice.infos = await db.models['invoiceOther'].findAll({
                    where: {
                        iid: id
                    }
                })
            }
            // vInvoice.records = await db.query(`select a.op,a.iid,a.uid,a.data,a.ctime,b.name from (SELECT op,iid,uid,data,ctime from invoice_log where iid=${id}) a left join admin b on a.uid=b.id`, { type: QueryTypes.SELECT })
            return ctx.suc('', vInvoice);
        }
    }
    catch (e) {
        return ctx.err(e);
    }
})

module.exports = router;